package com.hr;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.googlecode.cswish.annotation.ParamScope;
import com.googlecode.cswish.model.PageElements;
import com.googlecode.cswish.struts.spring.GenericService;

@Service
public class BankCollectionService {
	@Resource
	GenericService genericService;
	
	public void _update(BankCollection model, @ParamScope(visible=false, value="#ps")PageElements ps){
		ps.setElementReadOnly("model.acctTerm.id", true);
		ps.setElementReadOnly("model.state", false);
		ps.setElementReadOnly("model.itemCounts", true);
		ps.setElementReadOnly("model.amount", true);
		
		genericService.view(model);
	}
	
	@Transactional
	public void update(BankCollection model){
		BankCollection bankCollection = genericService.load(BankCollection.class, model.getId());
		List<BankCollectionItem> items = bankCollection.getChildren();
		if (items == null || items.isEmpty()) return;
		Integer state = model.getState();
		
		for(BankCollectionItem item : items){
			Salary salary = item.getSalary();
			Integer salaryState = Salary.STATE_BANKCOLLECTION;
			if (BankCollection.STATE_SUCCEED.equals(state)) {
				salaryState = Salary.STATE_SUCCEED;
			} else  {
				salaryState = Salary.STATE_BANKCOLLECTION;
			} 
			salary.setState(salaryState);
			genericService.merge(salary);
		}
		genericService.update(model);
	}
}
